package ru.jexp.rcp.supply.view;

import java.util.List;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

import ru.jexp.obekt.identifikacia.IIdentificiruemii;
import ru.jexp.rcp.supply.beans.IBeanConverter;
import ru.jexp.rcp.supply.swtext.IColumnCreator;
import ru.jexp.rcp.supply.viewer.JExpTableLabelProvider;

public abstract class TableViewPart extends AbstractViewPart<TableColumn, TableViewer>
{
	protected abstract JExpTableLabelProvider createTableLabelProvider();
	
	public TableViewPart(String idPart, String namePart, IBeanConverter converter)
	{
		super(idPart, namePart, converter, IColumnCreator.TABLE);
	}

	@Override
	protected TableViewer createViewer(Composite parent)
	{
		TableViewer viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
		viewer.setLabelProvider(createTableLabelProvider());
		viewer.setContentProvider(new ArrayContentProvider());
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
		return viewer;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void add(IIdentificiruemii liteBean, IObservableValue input)
	{
		wrapInput(input).add(liteBean);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void edit(IIdentificiruemii liteBean, IObservableValue input)
	{
		int index = wrapInput(input).indexOf(liteBean);
		wrapInput(input).set(index, liteBean);
	}
	
	@Override
	protected void remove(IIdentificiruemii liteBean, IObservableValue input)
	{
		wrapInput(input).remove(liteBean);
	}
	
	@SuppressWarnings("rawtypes")
	private List wrapInput(IObservableValue input)
	{
		return (List) input.getValue();
	}


}
